package de.uni_hamburg.informatik.mci.lineracalendar.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Scroller;

public class ViewCalenderOld extends View {

	private float scaleFactor = 1f;
	private static float MIN_ZOOM = 1f;
	private static float MAX_ZOOM = 5f;
	private static int MIN_SPANY = 10;
	private float mStartingSpanY = 0;
	private String TAG = "listeView";
	private int mCellHeightBeforeScallingGesture;
	private int mcellHeight = 100;
	private int cellHeightDay;
	private boolean mTouchStartedInAlldayArea = false;
	private int mViewWidth = 100;
	private Rect rect = new Rect();
	private float mx;
	private float my;
	private float[] lines = new float[16];
	private float[] mhours = new float[16];
	private int mHeightBeforeScaleGesture;
	// this is a test number, only for testing
	private int day = 31;
	private float[] mdays = new float[31 * 4 * 4];
	private boolean mStartingScroll;
	private float mInitialScrollY;
	private boolean mScrolling;

	private ScaleGestureDetector mScaleDetector;
	private GestureDetector mdetector;
	private Paint paint;
	private Paint p;
	private DayView dayView;
	private Rect mdestRect;
	private int mTouchMode = TOUCH_MODE_INITAL_STATE;
	private Scroller mScroller;
	private int mViewStartY;
	private int mMaxViewStartY;
	private final int OVERFLING_DISTANCE;
	private Handler mHandler;
	

	/**
	 * the inital state in te touch gesture
	 */
	private static final int TOUCH_MODE_INITAL_STATE = 0;

	/**
	 * indicate the scroll for touch gesture
	 */
	private static final int TOUCH_MODE_SCROLL = 0x20;

	/**
	 * indicate just we receive Touch event for scroll or other Gesture
	 */
	private static final int TOUCH_MODE_DOWN = 1;

	@SuppressLint("NewApi")
	public ViewCalenderOld(Context context) {
		super(context);
		mScaleDetector = new ScaleGestureDetector(context, new ScalerListner());
		mdetector = new GestureDetector(context, new DetectionListner());
		dayView = new DayView(context);
		mScroller = new Scroller(context);

		ViewConfiguration vc = ViewConfiguration.get(context);
		OVERFLING_DISTANCE = vc.getScaledOverflingDistance();

		init();
		// detector = new GestureDetector(context, new DetectionListner());

	}

	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.GRAY);
		paint.setStyle(Paint.Style.STROKE);

	}

	@SuppressLint({ "DrawAllocation", "WrongCall" })
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.save();

		// Rect rectdest = rect;
		// rectdest.bottom = bottome;
		// rectdest.top = 10;
		// rectdest.right = canvas.getWidth();
		// rectdest.left = 10;
		// Rect [] rect = new Rect[10];
		dodraw(canvas);
	}

	@SuppressLint("WrongCall")
	private void dodraw(Canvas canvas) {
		int lineIndex = 0;

		int border1 = 3;
		int border2 = 3;
		int area;
		for (int i = 0; i <= 3; i++) {
			lines[lineIndex++] = 10;
			lines[lineIndex++] = border2;
			lines[lineIndex++] = canvas.getWidth();
			lines[lineIndex++] = border2;
			// draw the 31 day for every Month
			border1 = border2;
			border2 += mcellHeight;
			area = calculateAvreage(border1, border2);
			if (area > 300) {
//				dayView.doDrawDay(canvas, area, border1);
			}

		}
		canvas.drawLines(lines, 0, lineIndex, paint);
	}

	// calculate the difference between two Y coordonne
	private int calculateAvreage(int y1, int y2) {
		return y2 - y1;
	}

	@Override
	protected void onAttachedToWindow() {
		if (mHandler == null) {
			mHandler = getHandler();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mScaleDetector.onTouchEvent(event);
		mdetector.onTouchEvent(event);
//		int action = event.getAction();
//		switch (action) {
//		case MotionEvent.ACTION_DOWN:
//			
//			return true;
//		case MotionEvent.ACTION_MOVE:
//			mdetector.onTouchEvent(event);
//			return true;
//
//		default:
//			break;
//		}
		return true;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = 24 * 100;
		setMeasuredDimension(width, height);

	}

	private class ScalerListner extends
			ScaleGestureDetector.SimpleOnScaleGestureListener {

		@SuppressLint("NewApi")
		@Override
		public boolean onScale(ScaleGestureDetector detector) {

			mcellHeight *= detector.getScaleFactor();
			cellHeightDay *= detector.getScaleFactor();

			// float spanY = (int) Math.max(MIN_SPANY,
			// Math.abs(detector.getCurrentSpanY()));//
			// (detector.getCurrentSpanY()));
			// mcellHeight = (int) (mCellHeightBeforeScallingGesture *spanY
			// /mStartingSpanY);

			invalidate();
			return true;
		}

		@SuppressLint("NewApi")
		@Override
		public boolean onScaleBegin(ScaleGestureDetector detector) {
			mStartingSpanY = Math.max(MIN_SPANY,
					Math.abs(detector.getCurrentSpanY()));
			mCellHeightBeforeScallingGesture = 50;
			return true;
		}

		//
		@Override
		public void onScaleEnd(ScaleGestureDetector detector) {
			mStartingSpanY = 0;

		}
	}

	private class DetectionListner extends
			GestureDetector.SimpleOnGestureListener {

		@Override
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			Log.e(TAG, " gesture detection onDown");
			if (!mScroller.isFinished()) {
				mScroller.abortAnimation();
			}
			return true;
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			Log.i(TAG, "scrolling the calender");
//			ViewCalender.this.doScroll(e1, e2, distanceX, distanceY);
			
//			 scrollBy(0, (int)distanceY);
			 
			return true;

		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {

			ViewCalenderOld.this.fling(e1, e2, velocityX, velocityY); 
			return true; 
		}

	}

	private void doScroll(MotionEvent e1, MotionEvent e2, float deltaX,
			float deltaY) {
		if (mStartingScroll) {
			mInitialScrollY = 0;
			mStartingScroll = false;
		}
		mInitialScrollY += deltaY;
		int distanceY = (int) mInitialScrollY;
		// mTouchMode = TOUCH_MODE_DOWN;
		mTouchMode = TOUCH_MODE_SCROLL;
		int absDistanceY = Math.abs(distanceY);

		mScrolling = true;
		invalidate();
	}

	public void fling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if ((mTouchMode & TOUCH_MODE_SCROLL) == 0) {
			Log.d(TAG, "do fling: No Fling");
			return;
		}
		int height = getHeight() - getPaddingBottom() - getPaddingTop();
		int bottom = getHeight();


		Log.d(TAG, "do fling: No Fling1");
		mTouchMode = TOUCH_MODE_INITAL_STATE;
		mScrolling = true;
		mScroller.fling(0, getScrollY(), 0, (int)velocityY, 0, 0,0,Math.max(0, bottom - height));
		

	}


	// private void remeasure(int with, int height) {
	//
	// }
	
	
	

}
